package com.itcast.demoone

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.itcast.demoone.bean.LoginRequest
import com.itcast.demoone.http.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HttpDemoActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_http_demo)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 1. 获取频道列表
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
              lifecycleScope.launch(context = Dispatchers.Main) {
                 // 1.1 切换到IO线程发起get请求
                 val res = withContext(Dispatchers.IO) {
                     RetrofitClient.apiService.getChannels()
                 }
                 // 1.2 处理结果
                 if(res.code == 10000){
                     val data = res.data.channels
                     findViewById<TextView>(R.id.channel).text = "获取频道列表成功, 频道名称: ${data.joinToString(", ") { it.name }}"
                 } else {
                     Toast.makeText(this@HttpDemoActivity, "获取频道列表失败", Toast.LENGTH_SHORT).show()
                 }
              }

            } catch (e: Exception){
                e.printStackTrace()
                Toast.makeText(this, "获取频道列表失败", Toast.LENGTH_SHORT).show()
            }
        }

        // 2. 登录
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                lifecycleScope.launch(context = Dispatchers.Main) {
                    // 2.1 创建登录参数
                    val loginRequest = LoginRequest("admin", "123456")
                    // 2.2 创建登录请求
                    val res = withContext(Dispatchers.IO) {
                        RetrofitClient.apiService.login(loginRequest)
                    }
                    // 2.3 处理结果
                    if(res.code == 10000){
                        val data = res.data.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, token: $data"
                    } else {
                        Toast.makeText(this@HttpDemoActivity, "登录失败", Toast.LENGTH_SHORT).show()
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(this@HttpDemoActivity, "登录失败", Toast.LENGTH_SHORT).show()
            }
        }
    }
}